package com.jlf.controller.sample;

import com.jlf.controller.sample.entity.Sample1Input;
import com.jlf.controller.sample.entity.criteria.Sample1Criteria;
import com.jlf.core.error.exception.NoObjectException;
import com.jlf.core.server.entity.DeleteRecordWrapper;
import com.jlf.core.server.entity.ListWrapper;
import com.jlf.sample.auto.generate.domain.Sample1DO;
import com.jlf.sample.auto.generate.entity.AddSample1Wrapper;
import com.jlf.sample.auto.generate.entity.EditSampleWrapper;
import com.jlf.sample.auto.generate.server.Sample1Server;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 样例1的接口控制器
 *
 * @author wujr
 * 2023/7/7
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/7 1.0 新增]
 */
@Tag(name = "样例1的接口验证")
@RestController
@RequestMapping("/rest/jlf/sample1")
@Slf4j
@RequiredArgsConstructor
public class Sample1Controller {
    private final Sample1Server sample1Server;


    @GetMapping(path = "/time")
    public LocalDateTime getTime(){
        return LocalDateTime.now();
    }


    @PostMapping
    public Sample1DO add(@Valid @RequestBody Sample1Input input) throws Exception{
        Sample1DO record = input.to();
        sample1Server.preAdd(record);
        return sample1Server.add(record);
    }
    @PostMapping(path = "/wrapper")
    public Sample1DO addWrapper(@Valid @RequestBody Sample1Input input) throws Exception{
        Sample1DO record = input.to();
        AddSample1Wrapper wrapper = new AddSample1Wrapper();
        wrapper.setRecord(record);
        sample1Server.preAdd(wrapper);
        sample1Server.add(wrapper);
        return wrapper.getRecord();
    }

    @PutMapping(path = "/{recordId}")
    public Sample1DO edit(@PathVariable("recordId") Serializable recordId,
                          @Valid @RequestBody Sample1Input input) throws Exception {
        Sample1DO original = sample1Server.getById(recordId);
        if (original == null){
            throw new NoObjectException(Sample1DO.class, "样例1");
        }
        Sample1DO record = input.to();
        record.setId(original.getId());
        sample1Server.preEdit(original, record);
        return sample1Server.edit(original, record);
    }

    @PutMapping(path = "/wrapper/{recordId}")
    public Sample1DO editWrapper(@PathVariable("recordId") Serializable recordId,
                                 @Valid @RequestBody Sample1Input input) throws Exception{
        Sample1DO original = sample1Server.getById(recordId);
        if (original == null){
            throw new NoObjectException(Sample1DO.class, "样例1");
        }
        Sample1DO record = input.to();
        record.setId(original.getId());
        EditSampleWrapper wrapper = new EditSampleWrapper();
        wrapper.setOriginal(original);
        wrapper.setRecord(record);
        sample1Server.preEdit(wrapper);
        sample1Server.edit(wrapper);
        return wrapper.getRecord();
    }

    @DeleteMapping(path = "/{recordId}")
    public void delete(@PathVariable("recordId") Serializable recordId) throws Exception{
        Sample1DO record = sample1Server.getById(recordId);
        if (record == null){
            throw new NoObjectException(Sample1DO.class, "样例1");
        }
        sample1Server.preDelete(record);
        sample1Server.delete(record);
    }
    @DeleteMapping(path = "/wrapper/{recordId}")
    public void deleteWrapper(@PathVariable("recordId") Serializable recordId) throws Exception{
        Sample1DO record = sample1Server.getById(recordId);
        if (record == null){
            throw new NoObjectException(Sample1DO.class, "样例1");
        }
        DeleteRecordWrapper<Sample1DO> wrapper = new DeleteRecordWrapper<>();
        wrapper.setRecord(record);
        sample1Server.preDelete(wrapper);
        sample1Server.delete(wrapper);
    }

    @GetMapping
    public ListWrapper<Sample1DO> query(Sample1Criteria input,
                                        @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                        @RequestParam(value = "pageSize", required = false) Integer pageSize){
        return sample1Server.query(input, pageNo, pageSize);
    }
    @GetMapping(path = "/{recordId}")
    public Sample1DO view(@PathVariable("recordId") Serializable recordId) throws Exception {
        Sample1DO record = sample1Server.getById(recordId);
        if (record == null) {
            throw new NoObjectException(Sample1DO.class, "样例1");
        }
        return record;
    }
}
